import api from "../../utils/api";
import Utils from "../../utils/utils";

// pages/card/card.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 0 as number,
    activeIndex: 0 as number,
    status: [
      {
        title: "未使用",
        list: [],
        type: 0,
        value: "VALIDATE",
        page: 1,
        totalCount: 0
      },
      {
        title: "已使用",
        list: [],
        type: 1,
        value: "USED",
        page: 1,
        totalCount: 0
      },
      {
        title: "已过期", list: [], type: 2, value: "EXPIRED", page: 1,
        totalCount: 0
      }
    ] as Status[],
    pageSize: 10 as number,
    isLoading: false as boolean,//是否正在加载
    typeMap: {
      "BREAK": "折扣券",
      "MINUS": "满减券",
      "EXCHANGE": "兑换券"
    },//类型映射表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getCouponList().then(() => {
      this.calcHeight();
    });
  },

  // 计算高度
  calcHeight(): void {
    let className;
    // 有数据就获取content的高度，无数据就获取empty的高度
    if (this.data.status[this.data.activeIndex].list.length !== 0) {
      className = "card_" + this.data.activeIndex
    } else {
      className = "empty"
    }
    const that = this;
    const systemInfo = wx.getSystemInfoSync();
    const scale = 750 / systemInfo.windowWidth;
    wx.createSelectorQuery().select(`.${className}`).boundingClientRect(function (rect) {
      that.setData({
        height: rect.height * scale
      })
    }).exec();
  },
  // 切换
  slideOn(e: any): void {
    this.setData({
      activeIndex: e.detail.current,
    })
    this.getCouponList().then(() => {
      this.calcHeight()
    });
  },
  changeItem(e: any): void {
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
    });
  },
  // 获取卡券列表
  async getCouponList(): Promise<void> {
    if (this.data.isLoading) {
      return
    }
    const { pageSize, status, activeIndex } = this.data
    const page = status[activeIndex].page
    const params = {
      page,
      pageSize,
      status: status[activeIndex].value
    }
    if(status[activeIndex].totalCount == status[activeIndex].list.length && status[activeIndex].totalCount != 0) {
      return
    }
    this.setData({
      isLoading: true
    })
    const res = await api.coupon.getCouponList(params)
    if (res.data.code == '000') {
      if (res.data.data && res.data.data.list) {
        res.data.data.list.forEach(item => {
          item.expireTime = Utils.timestampToDateTime(item.expireTime as number)
        })
        status[activeIndex].list = [...status[activeIndex].list, ...res.data.data.list],
        status[activeIndex].totalCount = res.data.data.totalCount as number
        status[activeIndex].page += 1
        this.setData({
          status,
        })
      }
      this.setData({
        isLoading: false
      })
    } else {
      this.setData({
        isLoading: false
      })
      wx.showToast({
        title: res.data.message as string,
        icon: "error"
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const {activeIndex, status } = this.data
    if (status[activeIndex].totalCount == status[activeIndex].list.length) {
      return
    }
    this.getCouponList().then(() => {
      this.calcHeight()
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})